import {combineReducers} from 'redux-immutable';
import Immutable from 'immutable';
import * as actions from './actions';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION} from './constants';
const EMPTY_ARRAY = [];
const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_BEGIN:
            return state.merge({
                isFetching: true,
                message: ''
            });

        case actions.GET_INIT_SUCCESS:
            return state.merge({
                isFetching: false,
                brands: action.data.brands || EMPTY_ARRAY,
                isDealerUser: action.data.isDealerUser,
                message: ''
            });

        case actions.GET_INIT_FAIL:
            return state.merge({
                message: action.message,
                isFetching: false
            });

        default:
            return state;
    }
};

const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                total: 0,
            });
        case actions.GET_LIST_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                message: '',
                data: action.data.content
            });
        case actions.GET_LIST_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                message: action.message,
                data: []
            });
        default:
            return state;
    }
};
export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};
const detail = (state, action) => {
    switch(action.type) {
        case actions.GET_DETAIL_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                data: {}
            });
        case actions.GET_DETAIL_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                data: action.data
            });
        case actions.GET_DETAIL_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message,
                data: {}
            });
        default:
            return state;
    }
};

const submitData = (state, action) => {
    switch(action.type) {
        case actions.GET_DETAIL_SUCCESS: {
            const {provinceId, cityId, countyId} = action.data;
            const region = [];
            if(provinceId)
                region.push(provinceId);
            if(cityId)
                region.push(cityId);
            if(countyId)
                region.push(countyId);
            action.data.region = region;
            return Immutable.fromJS(action.data);
        }
        case actions.GET_DETAIL_FAIL:
            return Immutable.Map();
        default:
            return state;
    }
};

const queryCondition = (state, action) => {
    switch(action.type) {
        case actions.SELECT_DEALER_QUERY:
            return state.merge({
                ...action.data
            });
        case actions.SAVE_QUERY_CONDITION:
            return state.set(action.name, action.value);
        case actions.RESET_QUERY_PANEL:
            return state.merge(DEFAULT_QUERY_OPTION);
        case actions.GET_LIST_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};

const queryBySort = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};


const domainData = combineReducers({
    list,
    permission,
    detail,
    initData,
    submitData
});
const appState = combineReducers({
    queryCondition,
    queryBySort,

});
export default combineReducers({
    domainData,
    appState,
});
